#include "CRoom.h"
#include "CEntity.h"
#include <vector>

using namespace std;

CRoom::CRoom() {}
CRoom::~CRoom() {}

void CRoom::step(const double &time) {
	for(size_t i = 0; i < entities.size(); ++i) {
		entities[i]->step(time);
		if(entities[i]->isDisposed()) {
			entities[i]->destroy();
			delete entities[i];
			entities.erase(entities.begin() + i);
			--i;
		}
	}
	/*for (vector<CEntity*>::iterator i = entities.begin(); i != entities.end(); i++) {
		
		(*i)->step(time);
		if ((*i)->isDisposed()) { //Dispose of object.
			(*i)->destroy();
			delete (*i);
			this->entities.erase(i);
			
		}
	}*/
}

void CRoom::draw(const double &time) {
	const VGCColor backgroundColor = VGCColor(255, 255, 255, 255);
	VGCDisplay::clear(backgroundColor);
	
	for (vector<CEntity*>::iterator i = entities.begin(); i != entities.end(); i++) {
		(*i)->draw(time);
	}
}

void CRoom::destroy() {
	for (vector<CEntity*>::iterator i = entities.begin(); i != entities.end(); i++) {
		(*i)->destroy();
		delete (*i);
	}
}

void CRoom::addEntity(CEntity &entity) {
	this->entities.push_back(&entity);
}

vector<CEntity*> CRoom::entitiesAt(CVector point) {
	vector<CEntity*> result;
	for (vector<CEntity*>::iterator i = entities.begin(); i != entities.end(); i++) {
		if ((*i)->isAt(point)) {
			result.push_back(*(i));
		}
	}
	return result;
}

